package com.kaifamiao.www.demo.do0902;

import java.sql.*;

public class JDBCTEST {
    public static void main(String[] args) {

        try {
            // 1,注册数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2 获取数据库连接
            String url = "jdbc:mysql://localhost:3306/kfm?user=root&password=";
            Connection conn=DriverManager.getConnection(url);
            System.out.println(conn);
            //3 获取发送执行SQL的对象(创建statement对象)
            Statement stat = conn.createStatement();
            //4 定义SQL语句
            String sql = """
                    insert into users(username,password) values("wangwr",145241)
                    """;
            //boolean result = stat.execute(sql);
            int result = stat.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS); // 影响的行数
            ResultSet generatedKeys = stat.getGeneratedKeys();
            while (generatedKeys.next()){
                int id = generatedKeys.getInt(1);
                System.out.println(id);
            }
            System.out.println(result);
            // 关闭连接
            stat.close();
            conn.close();
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
